package mobile.android.portal.webservice;

//import lk.icta.mobile.apps.postalcode.obj.Divisions;
import mobile.android.portal.obj.Categories;
import mobile.android.portal.obj.Materials;
import mobile.android.portal.obj.ProductByProductId;
import mobile.android.portal.obj.ProductInformation;
import mobile.android.portal.obj.ProductsByCategory;
import mobile.android.portal.obj.User;
import mobile.android.portal.util.Session;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

public class BusinessPortalWebService {

	private static final String NAMESPACE = "http://ws.wso2.org/dataservice";// "http://ws.wso2.org/dataservice";
	private static final String ENDPOINT = "http://10.0.2.2:9762/services/MobilePortalService.MobilePortalServiceHttpSoap11Endpoint";

	private static SoapObject callWebService(String actionName,
			SoapObject request) throws Exception {
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.setOutputSoapObject(request);
		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
				ENDPOINT);
		androidHttpTransport.call(actionName, envelope);
		Log.v("result", envelope.bodyOut.toString());
		SoapObject result = (SoapObject) envelope.bodyIn;
		return result;
	}

	private static SoapObject callUpdateWebService(String actionName,
			SoapObject request) throws Exception {
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.setOutputSoapObject(request);
		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
				ENDPOINT);
		androidHttpTransport.call(actionName, envelope);
		Log.v("result", envelope.bodyOut.toString());
		SoapObject result = (SoapObject) envelope.bodyOut;
		return result;
	}

	public static Categories getAllCategories() throws Exception {

		String methodName = "getAllCategories";
		String actionName = "getAllCategories";

		SoapObject request = new SoapObject(NAMESPACE, methodName);

		Log.v("epr", ENDPOINT);

		// request.addProperty("language", "en");

		SoapObject results = callWebService(actionName, request);
		Log.v("reult callws", results.toString());
		int length = results.getPropertyCount();

		Log.v("length", Integer.toString(length));

		Categories categories = new Categories();
		categories.setCount(length);
		categories.setCategoryIds(new String[length]);
		categories.setCategoryNames(new String[length]);

		for (int i = 0; i < length; i++) {
			SoapObject result = (SoapObject) results.getProperty(i);
			categories.getCategoryIds()[i] = String.valueOf(result.getProperty(
					"categoryId").toString());
			categories.getCategoryNames()[i] = String.valueOf(result
					.getProperty("categoryName").toString());
		}

		return categories;
	}

	public static ProductsByCategory getProductByCategory(int categoryId)
			throws Exception {

		String methodName = "getProductByCategory";
		String actionName = "getProductByCategory";

		SoapObject request = new SoapObject(NAMESPACE, methodName);
		request.addProperty("category_id", categoryId);
		Log.v("epr", ENDPOINT);

		// request.addProperty("language", "en");

		SoapObject results = callWebService(actionName, request);
		Log.v("reult callws", results.toString());
		int length = results.getPropertyCount();

		Log.v("length", Integer.toString(length));

		ProductsByCategory productByCategory = new ProductsByCategory();
		productByCategory.setProductCount(length);
		productByCategory.setProductIds(new String[length]);
		productByCategory.setProductNames(new String[length]);

		for (int i = 0; i < length; i++) {
			SoapObject result = (SoapObject) results.getProperty(i);
			productByCategory.getProductIds()[i] = String.valueOf(result
					.getProperty("product_id").toString());
			productByCategory.getProductNames()[i] = String.valueOf(result
					.getProperty("product_name").toString());
		}

		return productByCategory;
	}

	public static ProductByProductId getProductByProductId(int productId)
			throws Exception {

		String methodName = "getProductByProductId";
		String actionName = "getProductByProductId";

		SoapObject request = new SoapObject(NAMESPACE, methodName);

		Log.v("epr", ENDPOINT);
		request.addProperty("product_id", productId);

		SoapObject results = callWebService(actionName, request);
		int length = results.getPropertyCount();

		ProductByProductId productByProductId = new ProductByProductId();

		productByProductId.setCount(length);
		productByProductId.setProductIds(new String[length]);
		productByProductId.setProductNames(new String[length]);
		productByProductId.setCategoryIds(new String[length]);
		productByProductId.setMaterialIds(new String[length]);
		productByProductId.setProducerIds(new String[length]);
		productByProductId.setPrices(new String[length]);
		productByProductId.setQuantities(new String[length]);

		for (int i = 0; i < length; i++) {
			SoapObject productsByProductIdResult = (SoapObject) results
					.getProperty(i);
			productByProductId.getProductIds()[i] = String
					.valueOf(productsByProductIdResult
							.getProperty("product_id"));
			productByProductId.getProductNames()[i] = String
					.valueOf(productsByProductIdResult
							.getProperty("product_name"));
			productByProductId.getCategoryIds()[i] = String
					.valueOf(productsByProductIdResult
							.getProperty("category_id"));
			productByProductId.getMaterialIds()[i] = String
					.valueOf(productsByProductIdResult
							.getProperty("material_id"));
			productByProductId.getProducerIds()[i] = String
					.valueOf(productsByProductIdResult
							.getProperty("producer_id"));
			productByProductId.getPrices()[i] = String
					.valueOf(productsByProductIdResult.getProperty("price"));
			productByProductId.getQuantities()[i] = String
					.valueOf(productsByProductIdResult.getProperty("quantity"));

		}

		return productByProductId;

	}

	public static Materials getAllMaterials() throws Exception {

		String methodName = "getAllMaterials";
		String actionName = "getAllMaterials";

		SoapObject request = new SoapObject(NAMESPACE, methodName);

		Log.v("epr", ENDPOINT);

		// request.addProperty("language", "en");

		SoapObject results = callWebService(actionName, request);
		Log.v("reult callws", results.toString());
		int length = results.getPropertyCount();

		Log.v("length", Integer.toString(length));

		Materials materials = new Materials();
		materials.setCount(length);
		materials.setMaterialIds(new String[length]);
		materials.setMaterialNames(new String[length]);

		for (int i = 0; i < length; i++) {
			SoapObject result = (SoapObject) results.getProperty(i);
			materials.getMaterialIds()[i] = String.valueOf(result.getProperty(
					"material_id").toString());
			materials.getMaterialNames()[i] = String.valueOf(result
					.getProperty("material_name").toString());
		}

		return materials;
	}

	public static void addNewProduct(String productName, int categoryId,
			int materialId, int producerId, int price, int quantity)
			throws Exception {

		String methodName = "addNewProduct";
		String actionName = "addNewProduct";

		SoapObject request = new SoapObject(NAMESPACE, methodName);
		request.addProperty("product_name", productName);
		request.addProperty("category_id", categoryId);
		request.addProperty("material_id", materialId);
		request.addProperty("producer_id", producerId);
		request.addProperty("price", price);
		request.addProperty("quantity", quantity);

		Log.v("epr", ENDPOINT);

		// request.addProperty("language", "en");

		SoapObject results = callUpdateWebService(actionName, request);
		Log.v("reult callws", results.toString());
		int length = results.getPropertyCount();

		Log.v("length", Integer.toString(length));

		/*
		 * ProductsByCategory productByCategory = new ProductsByCategory();
		 * productByCategory.setProductCount(length);
		 * productByCategory.setProductIds(new String[length]);
		 * productByCategory.setProductNames(new String[length]);
		 * 
		 * for (int i = 0; i < length; i++) { SoapObject result = (SoapObject)
		 * results.getProperty(i); productByCategory.getProductIds()[i] =
		 * String.valueOf(result.getProperty("product_id").toString());
		 * productByCategory.getProductNames()[i] =
		 * String.valueOf(result.getProperty("product_name").toString()); }
		 */

		// return null;
	}

	public static void createAccount(String name, String organization, String address,
			String email, String telephn, String username, String password)
			throws Exception {
		
		
		String methodName = "addNewUser";
		String actionName = "addNewUser";

		SoapObject request = new SoapObject(NAMESPACE, methodName);
		request.addProperty("producer_name", name);
		request.addProperty("organization_name", organization);
		request.addProperty("address", address);
		request.addProperty("email", email);
		request.addProperty("telephone_number", telephn);
		request.addProperty("user_name", username);
		request.addProperty("password", password);

		Log.v("epr", ENDPOINT);

		// request.addProperty("language", "en");

		SoapObject results = callUpdateWebService(actionName, request);
		Log.v("reult callws", results.toString());
		int length = results.getPropertyCount();

		Log.v("length", Integer.toString(length));


	}
	
	
	public static User doLogin(String user_name,String password) throws Exception{
		
		String methodName = "doLogin";
		String actionName = "doLogin";
		SoapObject request = new SoapObject(NAMESPACE, methodName);
		request.addProperty("user_name", user_name);
		request.addProperty("password", password);
		
		
		Log.v("epr", ENDPOINT);
		
		SoapObject results = callWebService(actionName, request);
		Log.v("reult callws", results.toString());
		int length = results.getPropertyCount();
		Log.v("length", Integer.toString(length));
		User user=new User();

		if(length>0){
			
			//SoapObject result = (SoapObject) results.getProperty("user");
			
			

			//for (int i = 0; i < length; i++) {
				SoapObject result = (SoapObject) results.getProperty(0);
				user.setCount(length);
				user.setProducer_id(Integer.valueOf(result.getProperty("producer_id").toString()));
				user.setProducer_name(String.valueOf(result.getProperty("producer_name").toString()));
				user.setOrganization_name(String.valueOf(result.getProperty("organization_name").toString()));
				user.setAddress(String.valueOf(result.getProperty("address").toString()));
				user.setEmail(String.valueOf(result.getProperty("email").toString()));
				user.setTelephone(String.valueOf(result.getProperty("telephone_number").toString()));				
			//}
			//Log.v("user", user.getProducer_name());
			
		}	
		
		return user;
	}

}
